package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.HighLeverFaultDto;
import com.hualude.roadsign.dto.DTHighLeverFaultDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.repository.HighLeverFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.HighLeverFaultHandleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu_jiaHui on 2018/3/24.
 */
@Service
public class HighLeverFaultHandleServiceImpl implements HighLeverFaultHandleService {
    @Resource
    private HighLeverFaultRepository highLeverFaultRepository;
    @Resource
    private UserRepository userRepository;

    public DTResponseDto getHighLeverFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer machineMaintenanceStatus) throws Exception {
        List<DTHighLeverFaultDto> dto4DTs = highLeverFaultRepository.getHighLeverFaultHandle4DT(dtRequestDto,actorIds, orgId,machineMaintenanceStatus);
        Integer count = highLeverFaultRepository.getHighLeverFaultHandleCount4DT(dtRequestDto,actorIds, orgId,machineMaintenanceStatus);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for(DTHighLeverFaultDto dtMachineFaultDto:dto4DTs){
            if(dtMachineFaultDto.getHighLeverMaintainerId()!=null){
                String maintainerName =userRepository.getUserNameByUserId(dtMachineFaultDto.getHighLeverMaintainerId());
                dtMachineFaultDto.setHighLeverMaintainerName(maintainerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer addHighLeverMaintenanceContent(Integer machineFaultId, String machineMaintenanceContent) throws Exception {
        return highLeverFaultRepository.addHighLeverMaintenanceContent(machineFaultId,machineMaintenanceContent);
    }

    @Transactional
    public Integer finishHighLeverMaintenance(String machineMaintenancePhoto, Integer machineMaintainerId, String machineMaintenanceTime, String machineMaintenanceExplain, Integer machineFaultId) throws Exception {
        return highLeverFaultRepository.finishHighLeverMaintenance(machineMaintenancePhoto,machineMaintainerId,machineMaintenanceTime,machineMaintenanceExplain,machineFaultId);
    }


    public List<HighLeverFaultDto> getHighLeverFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception {
        return highLeverFaultRepository.getHighLeverFaultHandleList4App(orgId, maintenanceStatus, actorIds, count, size);
    }

    public HighLeverFaultDto getHighLeverReceiptInfo4App(Integer machineFaultId) throws Exception{
        return highLeverFaultRepository.getMachineReceiptInfo4App(machineFaultId);
    }
}
